package com.maezia.tdr.common.redis;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.*;

import javax.annotation.Resource;
import java.io.Serializable;

public class BaseRedisRepository <T extends Serializable> implements InitializingBean {

    protected RedisTemplate<String,T> redisTemplateTemporary;
    protected ValueOperations<String, T> redisValueOpsTemporary;
    protected HashOperations<String, String, T> redisHashOpsTemporary;
    protected SetOperations<String, T> redisSetOpsTemporary;
    protected ZSetOperations<String, T> redisZSetOpsTemporary;
    protected ListOperations<String, T> redisListOpsTemporary;

    @Resource(name="longRedisTemplate")
    protected RedisTemplate<String,Long> longRedisTemplate;
    protected ValueOperations<String, Long> longRedisValueOps;
    protected HashOperations<String, String, Long> longRedisHashOps;
    protected SetOperations<String, Long> longRedisSetOps;
    protected ZSetOperations<String, Long> longRedisZSetOps;
    protected ListOperations<String, Long> longRedisListOps;

    @Resource(name="stringRedisTemplate")
    protected StringRedisTemplate stringRedisTemplate;
    @Resource(name="stringRedisTemplate")
    protected ValueOperations<String, String> stringRedisValueOps;
    @Resource(name="stringRedisTemplate")
    protected HashOperations<String, String, String> stringRedisHashOps;
    @Resource(name="stringRedisTemplate")
    protected SetOperations<String, String> stringRedisSetOps;
    @Resource(name="stringRedisTemplate")
    protected ZSetOperations<String, String> stringRedisZSetOps;
    @Resource(name="stringRedisTemplate")
    protected ListOperations<String, String> stringRedisListOps;

    @Autowired
    private RedisConnectionFactory redisConnectionFactory;
    private Class<T> clazz;
    public BaseRedisRepository(Class<T> clazz) {
        this.clazz = clazz;
        redisTemplateTemporary = new RedisTemplate<String, T>();
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        redisTemplateTemporary.setConnectionFactory(redisConnectionFactory);
        FastJsonSerializer<T> stringRedisSerializer = new FastJsonSerializer<T>(clazz);
        redisTemplateTemporary.setHashKeySerializer(stringRedisSerializer);
        redisTemplateTemporary.setKeySerializer(stringRedisSerializer);
        redisTemplateTemporary.setValueSerializer(stringRedisSerializer);
        redisTemplateTemporary.setHashValueSerializer(stringRedisSerializer);
        redisTemplateTemporary.afterPropertiesSet();

        this.redisValueOpsTemporary = redisTemplateTemporary.opsForValue();
        redisHashOpsTemporary = redisTemplateTemporary.opsForHash();
        redisSetOpsTemporary = redisTemplateTemporary.opsForSet();
        redisZSetOpsTemporary = redisTemplateTemporary.opsForZSet();
        redisListOpsTemporary = redisTemplateTemporary.opsForList();

        longRedisValueOps = longRedisTemplate.opsForValue();
        longRedisHashOps = longRedisTemplate.opsForHash();
        longRedisSetOps = longRedisTemplate.opsForSet();
        longRedisZSetOps = longRedisTemplate.opsForZSet();
        longRedisListOps = longRedisTemplate.opsForList();
    }
}

